

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:o2oa_all_platform/common/extension/string_extension.dart';
import 'package:share_plus/share_plus.dart';

import '../../../common/utils/loading.dart';
import '../../../common/utils/log_util.dart';
import 'controller.dart';

/// 文件分享页面
class ShareFilePage extends StatelessWidget {

  final String shareFilePath;
  String? fileName;
  
  final controller = ShareFileController();

  ShareFilePage( {super.key, required this.shareFilePath, this.fileName});

  @override
  Widget build(BuildContext context) {
    if (fileName == null || fileName!.isEmpty) {
        fileName = shareFilePath.fileName();
    }
    return Scaffold(
      appBar: AppBar(
        title:   Text('common_file_share'.tr),
      ),
      body: SafeArea(
        child: Center(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              const SizedBox(height: 10),
              Text(fileName ?? '未知'),
              const SizedBox(height: 10),
              ElevatedButton(child:   Text('common_file_share_to_other'.tr), onPressed: () => shareFile(),)
            ],
          ),
        ),
      )
    );
  }

  Future<void> shareFile() async {
    final context = Get.context;
    if (context == null) {
      OLogger.e('context 为空！');
      return;
    }
    final box = context.findRenderObject() as RenderBox?;
    final result = await Share.shareXFiles([XFile(shareFilePath)],
        sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size);
    if (result.status != ShareResultStatus.success) {
      OLogger.i('分享失败，${result.status}');
      return;
    }
    Loading.toast('common_share_success'.tr);
  }
}